﻿using System;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;

namespace OpenShape
{
    public delegate void Deligate_FieldExpression(string txtExpression);

    public partial class Prop : Form
    {
        
        
        public event Deligate_ChangedLabelsProp prop_ChangedLabelsProp; // 델리게이트


        int Index;
        
        Font fonts = new Font(   //라벨폰트
        new FontFamily("Arial"),
        10,
        FontStyle.Regular,
        GraphicsUnit.Point);
        FontStyle pFontStyle = FontStyle.Regular;
        Color pFontColor = Color.Black;  //라벨폰트 색상
        Color pSelFonColor = Color.Black;   // 현재 창에서 선택되어진 라벨폰트 색상

        bool showLabelChk;


        // 생성자

        public Prop(Font p_fonts, Color p_FontColor, bool p_showLabelChk, int ID, string p_fieldname)
        {
            InitializeComponent();
            fonts = p_fonts;
            pFontColor = p_FontColor;
            tabLabel_Font.Text = fonts.FontFamily.Name;
            tabLabel_FontSize.Text = fonts.Size.ToString();
            pFontColor = pSelFonColor = p_FontColor;
            showLabelChk = p_showLabelChk;
            Index = ID;
            tabLabel_cmbField.Text = p_fieldname;
        }

        // Form_Load
        private void Prop_Load(object sender, EventArgs e)
        {
            InstalledFontCollection pFonts = new InstalledFontCollection();
            for (int i = 0; i < pFonts.Families .Length ; i++)
            {
                tabLabel_Font.Items .Add (pFonts .Families[i].Name);
            }
            tabLabel_pBoxFontColor.Invalidate();
            tabLabel_pBoxDisplayFont.Invalidate();
            tabLabel_chkLabelShow.Checked = showLabelChk;
            for (int i = 0; i < LayerManager.m_Layers[Index].m_DBF.pDataTable.Columns.Count; i++)
            {
                tabLabel_cmbField.Items.Add(LayerManager.m_Layers[Index].m_DBF.pDataTable.Columns[i].ColumnName.ToString());
            }
            tabLabel_cmbField.Text = LayerManager.m_Layers[Index].m_FieldName;

        }


        #region 라벨설정 메서드

        private void tabLabel_Font_SelectedValueChanged(object sender, EventArgs e)
        {
            tabLabel_pBoxDisplayFont.Invalidate();
        }

        private void tabLabel_pBoxFontColor_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush pBrush = new SolidBrush(pSelFonColor);
            g.Clear(SystemColors.ControlLightLight);
            g.FillRectangle(pBrush, 1, 1, tabLabel_pBoxFontColor.Width - 2, tabLabel_pBoxFontColor.Height - 2);
        }

        private void tabLabel_pBoxDisplayFont_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            SolidBrush pBrush = new SolidBrush(pSelFonColor);
            g.Clear(SystemColors.ControlLightLight);
            g.DrawString("가나다123ABCabc", new Font(new FontFamily(tabLabel_Font.Text), Convert.ToSingle(tabLabel_FontSize.Text), pFontStyle, GraphicsUnit.Point), new SolidBrush(pSelFonColor), ((float)tabLabel_pBoxFontColor.Height - fonts.Size) / 2, 2);
        }


        private void tabLabel_btnFontColor_Click(object sender, EventArgs e)
        {
            ColorDialog dlgColor = new ColorDialog();
            if (dlgColor.ShowDialog() == DialogResult.OK)
            {
                this.pSelFonColor = dlgColor.Color;
                tabLabel_pBoxFontColor.Invalidate();
                tabLabel_pBoxDisplayFont.Invalidate();
            }
        }

        private void tabLabel_btnFont_Click(object sender, EventArgs e)
        {
            FontDialog dlgFont = new FontDialog();
            if (dlgFont.ShowDialog() == DialogResult.OK)
            {
                tabLabel_Font.Text = dlgFont.Font.FontFamily.Name;
                tabLabel_FontSize.Text = dlgFont.Font.Size.ToString();
                pFontStyle = dlgFont.Font.Style;
                tabLabel_pBoxDisplayFont.Invalidate();
            }
        }


        private void tabLabel_btnExpression_Click(object sender, EventArgs e)
        {
            LabelExpression dlgLabelExpression = new LabelExpression(Index);
            dlgLabelExpression.LabelExpression_FieldExpression += new Deligate_FieldExpression(FieldExpression);
            dlgLabelExpression.Show();
        }


        private void tabLabel_btnApply_Click(object sender, EventArgs e)
        {
            fonts = new Font(new FontFamily(tabLabel_Font.Text), Convert.ToSingle(tabLabel_FontSize.Text ), pFontStyle, GraphicsUnit.Point);
            pFontColor = pSelFonColor;
            prop_ChangedLabelsProp(fonts, pFontColor, tabLabel_chkLabelShow.Checked, Index, tabLabel_cmbField.Text);
        }

        private void tabLabel_btnOK_Click(object sender, EventArgs e)
        {
            fonts = new Font(new FontFamily(tabLabel_Font.Text), Convert.ToSingle(tabLabel_FontSize.Text ), pFontStyle, GraphicsUnit.Point);
            pFontColor = pSelFonColor;
            prop_ChangedLabelsProp(fonts, pFontColor, tabLabel_chkLabelShow.Checked, Index, tabLabel_cmbField.Text);
            this.Dispose();
        }

        private void tabLabel_btnCancel_Click(object sender, EventArgs e)
        {
            //prop_ChangedLabelsProp(fonts, pFontColor, tabLabel_chkLabelShow.Checked, Index);
            this.Dispose();
        }

        private void FieldExpression(string txtExpression)
        {
            tabLabel_cmbField.Text = txtExpression;
        }

        #endregion

    }
}
